﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wechat.Util.Date
{
    public class DateTimeUtil
    {
        /// <summary>
        /// DateTime时间格式转换为Unix时间戳格式
        /// </summary>
        /// <param name="time"> DateTime时间格式</param>
        /// <returns>Unix时间戳格式</returns>
        public static string ConvertDateTime2String(System.DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return Convert.ToInt64((time - startTime).TotalSeconds).ToString();
        }

        /// <summary>
        /// Unix时间戳格式转换为DateTime时间格式
        /// </summary>
        /// <param name="timeStamp">Unix时间戳格式</param>
        /// <returns></returns>
        public static DateTime ConvertTimeStamp2DateTime(long unixTimeStamp)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return startTime.AddSeconds(unixTimeStamp); ;
        }
        /// <summary>
        /// 计算开始日期到结束日期之间的天数
        /// </summary>
        /// <param name="begin"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public static int GetDays(DateTime begin, DateTime end)
        {
            TimeSpan starTimeSpan = new TimeSpan(begin.Ticks);
            TimeSpan endTimeSpan = new TimeSpan(end.Ticks);
            int count = endTimeSpan.Days - starTimeSpan.Days;
            return count;
        }
    }
}
